package net.flyinggroup.vinsguru;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@Slf4j
public class FluxMono {
    @SneakyThrows
    public static void main(String[] args) {
        log.info("Starts");

        //flux emits one element per second
        Flux<Character> flux = Flux.just('a', 'b', 'c', 'd')
                .log()
                .delayElements(Duration.ofSeconds(1));
        //Observer 1 - takes 500ms to process
        flux
                .map(Character::toUpperCase)
                .subscribe(i -> {
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    log.info("Observer-1 : " + i);
                });
        //Observer 2 - process immediately
        flux.subscribe(i -> log.info("Observer-2 : " + i));

        log.info("Ends");

        //Just to block the execution - otherwise the program will end only with start and end messages
        sleep(10000);

    }
}
